$(document).ready(function() {
	Highcharts.setOptions({
		global: {
			useUTC: false
		}
	});

	var address = $('#address').val();
//	alert(address);
	var chart;
	$('#memory').highcharts({
		chart: {
			type: 'area',
			animation: Highcharts.svg,
			// don't animate in old IE
			marginRight: 0,
			events: {
				load: function() {
					// set up the updating of the chart each second
					var seriesUsed = this.series[0];
					var seriesRss = this.series[1];
					setInterval(function() {
						$.getJSON('/wsjc-web-redis-monitor/redis/getMemeryInfo?address=' + address,
						function(data) {
							var x = data.time,
							// current time
							y = data.used / 1024;
							seriesUsed.addPoint([x, y], true, true);
							y = data.rss / 1024;
							seriesRss.addPoint([x, y], true, true);
						});
					},
					2000);
				}
			}
		},
		title: {
			text: ''
		},
		xAxis: {
			type: 'datetime',
			tickPixelInterval: 100
		},
		yAxis: {
			title: {
				text: 'mem usage / kb'
			},
			plotLines: [{
				value: 0,
				width: 1,
				color: '#808080'
			}]
		},
		tooltip: {
			formatter: function() {
				return '<b>' + this.series.name + '</b><br/>' + Highcharts.numberFormat(this.y, 2) + '<br/>' + Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x);
			}
		},
		legend: {
			enabled: false
		},
		exporting: {
			enabled: false
		},
		series: [
			  {
				color: '#EEB422',
				name: 'used',
				data: (function() {
					var data = [],
					time = (new Date()).getTime(),
					i;
	
					for (i = -19; i <= 0; i++) {
						data.push({
							x: time + i * 1000,
							y: 0
						});
					}
					return data;
			 	})()
			 },
			 {
				color: '#48D1CC',
				name: 'rss',
				data: (function() {
					var data = [],
					time = (new Date()).getTime(),
					i;
	
					for (i = -19; i <= 0; i++) {
						data.push({
							x: time + i * 1000,
							y: 0
						});
					}
					return data;
			 	})()
			 }
		]
	});
	
	$('#cpu').highcharts({
		chart: {
//			type: 'spline',
			animation: Highcharts.svg,
			// don't animate in old IE
			marginRight: 10,
			events: {
				load: function() {
					// set up the updating of the chart each second
					var seriesUser = this.series[0];
					var seriesSys = this.series[1];
					setInterval(function() {
						$.getJSON('/wsjc-web-redis-monitor/redis/getCpuInfo?address=' + address,
						function(data) {
							var x = data.time,
							// current time
							 y = data.user;
							var z = data.sys;
//							alert(y);
							seriesUser.addPoint([x, y], true, true);
							y = data.sys;
							seriesSys.addPoint([x, y], true, true);
						});
					},
					2000);
				}
			}
		},
		title: {
			text: ''
		},
		xAxis: {
			type: 'datetime',
			tickPixelInterval: 100
		},
		yAxis: {
			title: {
				text: 'cpu usage'
			},
			plotLines: [{
				value: 0,
				width: 1,
				color: '#CAE1FF'
			}]
		},
		tooltip: {
			formatter: function() {
				return '<b>' + this.series.name + '</b><br/>' + this.y + '<br/>' + Highcharts.dateFormat('%H:%M:%S', this.x);
			}
		},
		
		legend: {
			enabled: false
		},
		exporting: {
			enabled: false
		},
		series: [
			  {
				color: '#EEB422',
				name: 'user',
				data: (function() {
					var data = [],
					time = (new Date()).getTime(),
					i;
	
					for (i = -19; i <= 0; i++) {
						data.push({
							x: time + i * 1000,
							y: 0
						});
					}
					return data;
			 	})()
			 },
			 {
				color: '#48D1CC',
				name: 'sys',
				data: (function() {
					var data = [],
					time = (new Date()).getTime(),
					i;
	
					for (i = -19; i <= 0; i++) {
						data.push({
							x: time + i * 1000,
							y: 0
						});
					}
					return data;
			 	})()
			 }
		]
	});
	
	$('#command').highcharts({
		chart: {
			type: 'spline',
			animation: Highcharts.svg,
			// don't animate in old IE
			marginRight: 0,
			events: {
				load: function() {
					// set up the updating of the chart each second
					var seriesCmd = this.series[0];
					setInterval(function() {
						$.getJSON('/wsjc-web-redis-monitor/redis/getCmdInfo?address=' + address,
						function(data) {
							var x = data.time,
							// current time
							y = data.cmd;
							seriesCmd.addPoint([x, y], true, true);
						});
					},
					2000);
				}
			}
		},
		title: {
			text: ''
		},
		xAxis: {
			type: 'datetime',
			tickPixelInterval: 100
		},
		yAxis: {
			title: {
				text: 'Commonds / sec'
			},
			plotLines: [
				{
					value: 0,
					width: 1,
					color: '#808080'
			     }
			]
		},
		tooltip: {
			formatter: function() {
				return '<b>' + this.series.name + '</b><br/>' + Highcharts.numberFormat(this.y, 2) + '<br/>' + Highcharts.dateFormat('%H:%M:%S', this.x);
			}
		},
		legend: {
			enabled: false
		},
		exporting: {
			enabled: false
		},
		series: [{
			name: 'cmd',
			data: (function() {
				var data = [],
				time = (new Date()).getTime(),
				i;

				for (i = -19; i <= 0; i++) {
					data.push({
						x: time + i * 1000,
						y: Math.random() * (1000 - 800) + 800
					});
				}
				return data;
			})()
		}]
	});
	
	
});

function empty() {
	$.confirm({
        title: '确认',
        content: '确认要删除所有的日志信息?',
        type: 'white',
//        icon: 'glyphicon glyphicon-question-sign',
        buttons: {
            ok: {
                text: '确认',
                btnClass: 'btn-primary',
                action: function() {
//                    location.href = url; //指向下载资源（此处为目标文件的输出数据流）
                	$.ajax({
            			type: "POST",
            			url: "/wsjc-web-redis-monitor/redis/logEmpty?address=" + address,
            			success: function(result) {
            				if (result == "OK") {
            					 window.location.reload();
            				} 
            				/*else {
            					parent.layer.msg('日志清空失败，请重试！', {
            						icon: 5,
            						anim: 6,
            						offset: 't'
            					});
            				}*/
            			}
            		});
                }
            },
            cancel: {
                text: '取消',
                btnClass: 'btn-danger',
            	action: function() {
//	                    location.href = url; //指向下载资源（此处为目标文件的输出数据流）
                	return ;
                }
            }
        }
    });
}

/*$(document).ready(function() {
	       chart = {
		      type: 'area'
		   },
		   title = {
		      text: 'US and USSR nuclear stockpiles'   
		   },
		   subtitle = {
		      text: 'Source: <a href="http://thebulletin.metapress.com/content/c4120650912x74k7/fulltext.pdf">' +
		         'thebulletin.metapress.com</a>'  
		   },
		   xAxis = {
		      allowDecimals: false,
		      labels: {
		         formatter: function () {
		            return this.value; // clean, unformatted number for year
		         }
		      }
		   },
		   yAxis = {
		      title: {
		         text: 'Nuclear weapon states'
		      },
		      labels: {
		         formatter: function () {
		            return this.value / 1000 + 'k';
		         }
		      }
		   },
		   tooltip = {
		      pointFormat: '{series.name} produced <b>{point.y:,.0f}</b><br/>warheads in {point.x}'
		   },
		   plotOptions = {
		      area: {
		         pointStart: 1940,
		         marker: {
		            enabled: false,
		            symbol: 'circle',
		            radius: 2,
		            states: {
		               hover: {
		                 enabled: true
		               }
		            }
		         }
		      }
		   },
		   series: [
				  {
					color: '#EEB422',
					name: 'used',
					data: (function() {
						var data = [],
						time = (new Date()).getTime(),
						i;
		
						for (i = -19; i <= 0; i++) {
							data.push({
								x: time + i * 1000,
								y: 0
							});
						}
						return data;
				 	})()
				 },
				 {
					color: '#48D1CC',
					name: 'rss',
					data: (function() {
						var data = [],
						time = (new Date()).getTime(),
						i;
		
						for (i = -19; i <= 0; i++) {
							data.push({
								x: time + i * 1000,
								y: 0
							});
						}
						return data;
				 	})()
				 }
			],
		    events: {
				load: function() {
					// set up the updating of the chart each second
					var seriesUsed = this.series[0];
					var seriesRss = this.series[1];
					setInterval(function() {
						$.getJSON('/mybatisplus-spring-boot/redis/getMemeryInfo',
						function(data) {
							var x = data.time,
							// current time
							y = data.used / 1024;
							seriesUsed.addPoint([x, y], true, true);
							y = data.rss / 1024;
							seriesRss.addPoint([x, y], true, true);
						});
					},
					2000);
				}
			},
		   var json = {};   
		   json.chart = chart; 
		   json.title = title;   
		   json.subtitle = subtitle; 
		   json.tooltip = tooltip;
		   json.xAxis = xAxis;
		   json.yAxis = yAxis;  
//		   json.series = series;
		   
		   json.plotOptions = plotOptions;
		   $('#memory').highcharts(json);
		   
});	   */
		   
